package com.siecomzg.transport.handler;

import android.os.Bundle;

import com.siecomzg.proto.Message;
import com.siecomzg.tool.LogUtil;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.siecomzg.bean.BankCardInfoBean;
import com.siecomzg.bean.LoadLogBean;
import com.siecomzg.bean.TradeLogBean;
import com.siecomzg.constconfig.ConstDefine;
import com.siecomzg.transport.CallbackMap;
import com.siecomzg.transport.CallbackTask;
import com.siecomzg.transport.CommonClient;
import com.siecomzg.transport.DeviceCallback;
import com.siecomzg.transport.SiecomDevice;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created on 2017/1/16.
 */

public class FinanHandler extends CommonHandler {

    private final static String  tag = "FinanHandler";

    @Override
    public void onReceive(CommonClient server, Message.siecomproto msg) {

        String msgid = msg.getMsgid();
        HashMap<String, CallbackTask> callbackTaskHashMap = CallbackMap.getCallbackTaskHashMap();
        if(callbackTaskHashMap.containsKey(msgid)){
            CallbackTask callbackTask = callbackTaskHashMap.get(msgid);
            DeviceCallback callback = callbackTask.getCallback();
            callbackTask.getTimer().cancel();
            boolean isOnMain = callbackTask.isOnMain();
            int respondCmd = msg.getCmd();
            if(respondCmd== ConstDefine.IC_RESPOND||respondCmd== ConstDefine.PIC_RESPOND||
                    respondCmd== ConstDefine.MSR_RESPOND||respondCmd== ConstDefine.AUTO_RESPOND) {
                readBankCard(server, msg,callback,isOnMain);
            }else if(respondCmd==ConstDefine.IC_LOG_RESPOND||respondCmd == ConstDefine.PIC_LOG_RESPOND||respondCmd ==ConstDefine.AUTO_LOG_RESPOND){
                readLog(server, msg,callback,isOnMain);
            }else if(respondCmd==ConstDefine.ARQC_RESPOND){
                ARPCExecuteScript(msg,callback,isOnMain);
            }else if(respondCmd==ConstDefine.IC_LOAD_RESPOND||respondCmd == ConstDefine.PIC_LOAD_RESPOND||respondCmd ==ConstDefine.AUTO_LOAD_RESPOND){

                readLoadLog(server, msg,callback,isOnMain);
            }
            if(respondCmd==ConstDefine.CANCEL_FINAN_RESPOND ||
                    respondCmd == ConstDefine.ICCard_POWER_ON_RESPOND ||
                    respondCmd == ConstDefine.ICCard_POWER_OFF_RESPOND ||
                    respondCmd == ConstDefine.APDU_COMMAND_RESPOND ||
                    respondCmd == ConstDefine.READ_SOCIAL_CARD_RESPOND ||
                    respondCmd == ConstDefine.AUTO_CHECK_RESPOND   // 自动检测卡
            ){
                commonRespond(msg,callback, isOnMain);
            }

            callbackTaskHashMap.remove(msgid);
        }

    }
    private void readLoadLog(CommonClient server, Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {

        int errno = msg.getError();

        Bundle bundle = new Bundle();
        if (errno == 0) {
            Gson gson = new Gson();
            List<LoadLogBean> list= gson.fromJson(msg.getRespond(),new TypeToken<List<LoadLogBean>>() {}.getType());
            bundle.putParcelableArrayList("data", (ArrayList<LoadLogBean>) list);
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);

        } else {

            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }


    private void commonRespond(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        Bundle bundle = new Bundle();
        if (errno == 0) {
            bundle.putString("data",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }
    private void ARPCExecuteScript(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain){

        int errno = msg.getError();
        LogUtil.e("errno","=="+errno);
        Bundle bundle = new Bundle();
        if (errno == 0) {
            JSONObject json = null;
            try {
                json = new JSONObject(msg.getRespond());
                bundle.putInt("result",json.getInt("result"));
                bundle.putString("TC", json.optString("TC"));
                bundle.putString("ScriptResult", json.optString("ScriptResult"));

            } catch (JSONException e) {
                e.printStackTrace();
            }

            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);

        } else {

            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }
    private void readLog(CommonClient server, Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {

        int errno = msg.getError();

        Bundle bundle = new Bundle();
        if (errno == 0) {
            Gson gson = new Gson();
            List<TradeLogBean> list= gson.fromJson(msg.getRespond(),new TypeToken<List<TradeLogBean>>() {}.getType());
            bundle.putParcelableArrayList("data", (ArrayList<TradeLogBean>) list);
            bundle.putString("dataStr", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);

        } else {

            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }

    public void readBankCard(CommonClient server, Message.siecomproto msg,DeviceCallback callback,boolean isOnMain){
        int errno = msg.getError();

        Bundle bundle = new Bundle();
        if (errno == 0) {
            Gson gson = new Gson();
            BankCardInfoBean bean = gson.fromJson(msg.getRespond(), BankCardInfoBean.class);
            if(bean.cardNo == null && bean.ICChipData==null){
                try {
                    JSONObject jb = new JSONObject(msg.getRespond());
                    if(jb.has("pan")){
                        bean.cardNo = jb.getString("pan");
                    }
                    if(jb.has("message55")){
                        bean.ICChipData = jb.getString("message55");
                    }
                    if(jb.has("message35")){
                        bean.twoMagneticTrack = jb.getString("message35");
                    }
                    if(jb.has("track1")){
                        bean.cardType = ConstDefine.MSR_CARD;
                        bean.oneMagneticTrack = jb.getString("track1");
                    }
                    if(jb.has("track2")){
                        bean.twoMagneticTrack = jb.getString("track2");
                    }
                    if(jb.has("track3")){
                        bean.cardType = ConstDefine.MSR_CARD;
                        bean.threeMagneticTrack = jb.getString("track3");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            bundle.putParcelable("data", bean);
            bundle.putString("dataStr", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);

        } else {

            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }


}
